import { createRouter, createWebHistory } from 'vue-router'
import Cinemas from '@/views/Cinemas'
import Move from '@/views/Move'
import Users from '@/views/Users'
import NowPlaying from '@/components/move/NowPlaying'
import WillPlaying from '@/components/move/WillPlaying'
import Citys from '@/components/cinemas/Citys'
import AboutView from '@/views/AboutView'
import CinemasSearch from '@/components/cinemas/CinemasSearch'
import Detail from '@/components/move/Detail'
const routes = [
  {
    path: '/',
    redirect: {
      name: 'nowPlaying',
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '/order',
    component: ()=> import('@/views/Order'),
    meta:{
      isRequired: true
    }
  },
  {
    path: '/cinemas',
    name: 'cinemas',
    component: Cinemas
  },
  {
    path: '/citys',
    component: Citys
  },
  {
    path: '/cinemasSearch',
    component: CinemasSearch
  },
  {
    path: '/detail/:filmId',
    component: Detail
  },
  {
    path: '/move',
    name: 'move',
    component: Move,
    children: [
      {
        path: '/move/nowPlaying',
        name: 'nowPlaying',
        component: NowPlaying
      },
      {
        path: '/move/willPlaying',
        name: 'willPlaying',
        component: WillPlaying
      },
      {
        path: '/move',
        redirect: {
          name: 'nowPlaying',
        },

      }
    ]
  },
  {
    path: '/users',
    name: 'users',
    component: Users
  },
  {
    path: '/aboutView',
    name: 'aboutView',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: AboutView
  },
]
const router = createRouter({
  history: createWebHistory(),// 路由模式 history模式
  // history: createWebHashHistory(),// 路由模式 hash模式
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.isRequired){ // 判断是不是特殊路由
    if(sessionStorage.getItem('token')){ // 判断是否已经登陆，登录则放行
      next()
    }else{ //未登录则跳转道登录界面，并且记录是从何处跳转到登录界面的
      next({
        path:'/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  }else{ //普通路由直接放行
    next()
  }
})
export default router
